var WxParse = require("../../../wxParse/wxParse.js"), gSpecificationsModel = require("../../../components/goods/specifications_model.js"), goodsBanner = require("../../../components/goods/goods_banner.js"), goodsInfo = require("../../../components/goods/goods_info.js"), goodsBuy = require("../../../components/goods/goods_buy.js");

Page({
    data: {
        pageType: "INTEGRAL",
        hide: "hide",
        tab_detail: "active",
        tab_comment: "",
        poster_status: {},
        swiperIndex: 0,
        msgList: [],
        is_distributor: '',

        form: {
            number: 1
        },
        attr:[],
        btn: true,
        jf: 1
    },

    // 轮播特效果一
    swiperChange(e) {
        this.setData({
            swiperIndex: e.detail.current
        })
    },
    loadData: function () {
        var a = this;
        getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        }), getApp().request({
            url: getApp().api.default.get_poster_status,
            success: function (e) {
                console.log('tui=====', e)
                if (0 == e.code) {
                    a.setData({
                        poster_status: e.data
                    })
                }
            },
            complete: function () {
                getApp().core.hideLoading();
            }
        });
    },
    onLoad: function (t) {
        getApp().page.onLoad(this, t);
        var a = this;
        a.order_text_slide()
        if (t.scene) {
            var sdsd = decodeURIComponent(t.scene);
            var sss = getApp().helper.scene_decode(sdsd);
            console.log('ssssssssssss++', sss)
            sss.gid && (a.setData({
                id: sss.gid
            }), a.getGoods());
        } else {
            t.integral && a.setData({
                user_integral: t.integral
            }), t.goods_id && (a.setData({
                id: t.goods_id
            }), a.getGoods());
        }

        if (wx.getStorageSync('ACCESS_TOKEN')) {
            console.log('已登录')
            getApp().page.bindParent1(wx.getStorageSync('PARENT_ID'))
        } else {
            console.log('未登录')
            var recommender_id = wx.getStorageSync('recommender_id')
            console.log('recommender_id==', recommender_id)
            var sdsd = decodeURIComponent(t.scene);
            if (t.user_id) {
                if (recommender_id === '' || recommender_id === '0') {
                    console.log('wu')
                    wx.setStorageSync('recommender_id', t.user_id)
                } else {
                    console.log('you')
                }
            }
            if (sdsd) {
                var recommender = wx.getStorageSync('recommender_id')
                var sss = getApp().helper.scene_decode(sdsd);
                console.log('dsdsds', sss, 'recommender_id', recommender)
                if (recommender === '' || recommender === '0') {
                    console.log('wu')
                    wx.setStorageSync('recommender_id', sss.uid)
                } else {
                    console.log('you')
                }
            }
        }
    },
    order_text_slide: function (e) {
        var a = this
        getApp().request({
            url: getApp().api.default.order_text_slide,
            success: function (t) {
                if (t.code == 0) {
                    a.setData({
                        msgList: t.data
                    })
                }
            },
        });
    },
    getGoods: function () {
        var o = this;
        console.log('getgoods====', o.data.id)
        getApp().core.showLoading({
            title: "正在加载",
            mask: !0
        }), getApp().request({
            url: getApp().api.integral.goods_info,
            data: {
                id: o.data.id
            },
            success: function (t) {
                if (0 == t.code) {
                    var a = t.data.goods.detail;
                    WxParse.wxParse("detail", "html", a, o), getApp().core.setNavigationBarTitle({
                        title: t.data.goods.name
                    });
                    var e = t.data.goods;
                    e.num = e.goods_num, e.pic_list = e.goods_pic_list, e.min_price = 0 < e.price ? e.integral + "爱心 ￥" + e.price : e.integral + "爱心";
                    if (t.data.user != null) {
                        o.setData({
                            is_distributor: t.data.user.is_distributor
                        })
                    }
                    o.setData({
                        goods: t.data.goods,
                        attr_group_list: t.data.attr_group_list,
                        attr:t.data.goods.attr

                    }), o.selectDefaultAttr();
                } else getApp().core.showModal({
                    title: "提示",
                    content: t.msg,
                    showCancel: !1,
                    success: function (t) {
                        t.confirm && getApp().core.navigateTo({
                            url: "/pages/integral-mall/index/index"
                        });
                    }
                });
            },
            complete: function (t) {
                setTimeout(function () {
                    getApp().core.hideLoading();
                }, 500);
            }
        });
    },
    showShareModal: function () {
        this.setData({
            share_modal_active: "active",
            no_scroll: !0
        });
    },
    shareModalClose: function () {

        this.setData({
            share_modal_active: "",
            no_scroll: !1
        });
    },
    exchangeGoods: function () {
        var t = this;
        if (!t.data.show_attr_picker) return t.setData({
            show_attr_picker: !0
        }), !0;
        var a = t.data.attr_group_list, e = [];
        for (var o in a) {
            var i = !1;
            for (var n in a[o].attr_list) if (a[o].attr_list[n].checked) {
                i = {
                    attr_id: a[o].attr_list[n].attr_id,
                    attr_name: a[o].attr_list[n].attr_name
                };
                break;
            }
            if (!i) return getApp().core.showToast({
                title: "请选择" + a[o].attr_group_name,
                image: "/images/icon-warning.png"
            }), !0;
            e.push({
                attr_group_id: a[o].attr_group_id,
                attr_group_name: a[o].attr_group_name,
                attr_id: i.attr_id,
                attr_name: i.attr_name
            });
        }
        var r = t.data.user_integral, s = t.data.attr_integral, g = t.data.attr_num;
        // if (parseInt(r) < parseInt(s)) return getApp().core.showToast({
        //     title: "爱心不足!",
        //     image: "/images/icon-warning.png"
        // }), !0;
        if (g <= 0) return getApp().core.showToast({
            title: "商品库存不足!",
            image: "/images/icon-warning.png"
        }), !0;
        var d = t.data.goods, p = t.data.attr_price;
        s = t.data.attr_integral;
        console.log(JSON.stringify({
            goods_id: d.id,
            attr: e,
            attr_price: p,
            attr_integral: s
        }))
        t.setData({
            show_attr_picker: !1
        }), getApp().core.navigateTo({
            url: "/pages/integral-mall/order-submit/index?goods_info=" + JSON.stringify({
                goods_id: d.id,
                attr: e,
                attr_price: p,
                attr_integral: s,
                num: t.data.form.number,
            })
        });
    },
    onReady: function (t) {
        getApp().page.onReady(this);
    },
    onShow: function (t) {
        getApp().page.onShow(this), gSpecificationsModel.init(this), goodsBanner.init(this),
            goodsInfo.init(this), goodsBuy.init(this), this.loadData();
    },
    onHide: function (t) {
        getApp().page.onHide(this);
    },
    onUnload: function (t) {
        getApp().page.onUnload(this);
    },
    onPullDownRefresh: function (t) {
        getApp().page.onPullDownRefresh(this);
    },
    onReachBottom: function (t) {
        getApp().page.onReachBottom(this);
    },
    onShareAppMessage: function () {
        getApp().page.onShareAppMessage(this);
        var t = getApp().getUser(), ss = this, e = "", a = getApp().core.getStorageSync(getApp().const.WX_BAR_TITLE);
        for (var n in a) if (a[n] && "pages/tuibao-integral-mall/index/index" === a[n].url) {
            e = a[n].title;
            break;
        }
        return {
            path: "/pages/integral-mall/goods-info/index?goods_id=" + this.data.id + "&integral=" + this.data.user_integral + "&user_id=" + t.id,
            title: ss.data.goods.name
        };
    },

});